//
//  XNMainViewController.m
//  XINNingOA
//
//  Created by 葛承铭 on 2021/7/11.
//

#import "XNMainViewController.h"
#import "XNBillCountRequest.h"
#import "XNLoginOutRequest.h"
#import "XNBillTypeTableViewCell.h"
#import "XNBillInfoTableViewCell.h"
#import "XNLoginViewController.h"
#import "XNHud.h"
#import "XNUtil.h"
#import "SVProgressHUD.h"

@interface XNMainViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak) UIScrollView *contentScrollView;

@property (nonatomic, copy) NSArray *billTypeArray;

@property (nonatomic, copy) NSArray *billItemArray;

@property (nonatomic, weak) UITableView *tableTypeTableView;

@property (nonatomic, weak) UITableView *tableListTableView;

@property (nonatomic, weak) UIButton *leftBtn;

@property (nonatomic, weak) UIView *noDataView;


@end

@implementation XNMainViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getData];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    // app名称

    NSString *app_Name = [infoDic objectForKey:@"CFBundleDisplayName"];
    self.title = app_Name;
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 48, 48);
    [button setImage:[UIImage imageNamed:@"list"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.leftBtn = button;
    button.hidden = YES;
    
    UIButton * loginoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginoutBtn.frame = CGRectMake(0, 0, 48, 48);
    [loginoutBtn setImage:[UIImage imageNamed:@"loginout"] forState:UIControlStateNormal];
    [loginoutBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [loginoutBtn addTarget:self action:@selector(loginOut:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:loginoutBtn];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    contentScrollView.scrollEnabled = NO;
    [self.view addSubview:contentScrollView];
    self.contentScrollView = contentScrollView;
    [contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self addContent];
    [SVProgressHUD show];
    XNBillCountRequest *billCountRequest = [[XNBillCountRequest alloc] init];
    [billCountRequest startWithCompletionBlockWithSuccess:^(__kindof XNBillCountRequest * _Nonnull request) {
        [SVProgressHUD dismiss];
        self.billTypeArray = request.billArray;
        [self.tableTypeTableView reloadData];

    } failure:^(__kindof XNBillCountRequest * _Nonnull request) {
        [SVProgressHUD dismiss];
    }];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(getData) userInfo:nil repeats:YES];
    
}

- (void)getData
{
    NSLog(@"abc");
    XNBillCountRequest *billCountRequest = [[XNBillCountRequest alloc] init];
    [billCountRequest startWithCompletionBlockWithSuccess:^(__kindof XNBillCountRequest * _Nonnull request) {
        self.billTypeArray = request.billArray;
        [self.tableTypeTableView reloadData];
        self.billItemArray = [NSMutableArray array];
        [self.tableListTableView reloadData];
        self.noDataView.hidden = NO;
        [self selectType:nil];
        
//        if (self.selectBillCode.length > 0) {
//            [self.billTypeArray enumerateObjectsUsingBlock:^(XNBillCountModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                if ([obj.billCode isEqualToString:self.selectBillCode]) {
//                    self.billItemArray = obj.itemList;
//                    [self.tableListTableView reloadData];
//                    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:idx inSection:0];
//                    [self.tableTypeTableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
//                    *stop = YES;
//                }
//            }];
//        }
//        [SVProgressHUD dismiss];
    } failure:^(__kindof XNBillCountRequest * _Nonnull request) {
//        [SVProgressHUD dismiss];
    }];
}

- (void)addContent{
    UITableView *tableTypeTableView = [[UITableView alloc] init];
    tableTypeTableView.dataSource = self;
    tableTypeTableView.delegate = self;
    tableTypeTableView.rowHeight = 50;
    tableTypeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableTypeTableView.showsVerticalScrollIndicator = NO;
    [self.contentScrollView addSubview:tableTypeTableView];
    self.tableTypeTableView = tableTypeTableView;
    [tableTypeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(self.contentScrollView);
        make.height.equalTo(self.view);
        make.width.mas_equalTo(208);
    }];
    
    UILabel *dividLine = [[UILabel alloc] init];
    dividLine.backgroundColor = [UIColor colorWithHexString:@"#E0E0E0"];
    [self.contentScrollView addSubview:dividLine];
    [dividLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(tableTypeTableView);
        make.bottom.equalTo(self.contentScrollView);
        make.width.mas_equalTo(1);
        make.top.equalTo(self.contentScrollView);
    }];
    
    UITableView *tableListTableView = [[UITableView alloc] init];
    tableListTableView.dataSource = self;
    tableListTableView.delegate = self;
    tableListTableView.rowHeight = 120;
    tableListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableListTableView.showsVerticalScrollIndicator = NO;
    [self.contentScrollView addSubview:tableListTableView];
    self.tableListTableView = tableListTableView;
    [tableListTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tableTypeTableView.mas_right);
        make.top.bottom.right.equalTo(self.contentScrollView);
        make.width.mas_equalTo(SCREENW);
    }];
    self.tableTypeTableView.userInteractionEnabled = YES;
    self.tableListTableView.userInteractionEnabled = NO;
    
    UIView *noDataView = [[UIView alloc] init];
    [self.tableListTableView addSubview:noDataView];
    self.noDataView = noDataView;
    [noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.equalTo(self.tableListTableView);
        make.size.mas_equalTo(CGSizeMake(120, 160));
    }];
    
    UIImageView *noDataImageView = [[UIImageView alloc] init];
    noDataImageView.image = [UIImage imageNamed:@"no-data"];
    [noDataView addSubview:noDataImageView];
    [noDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.centerX.equalTo(noDataView);
        make.size.mas_equalTo(CGSizeMake(120, 120));
    }];
    
    UILabel *noDataLab = [[UILabel alloc] init];
    noDataLab.text = @"暂无数据";
    noDataLab.textAlignment = NSTextAlignmentCenter;
    noDataLab.font = [UIFont systemFontOfSize:18];
    [noDataView addSubview:noDataLab];
    [noDataLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(noDataView);
        make.height.mas_equalTo(20);
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableTypeTableView) {
        return self.billTypeArray.count;
    }else{
        return self.billItemArray.count;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableTypeTableView) {
        XNBillTypeTableViewCell *cell = [XNBillTypeTableViewCell tabelViewCellWithTableView:tableView ForIndexPath:indexPath];
        cell.billTypeModel = self.billTypeArray[indexPath.row];
        return cell;
    }else{
        XNBillInfoTableViewCell *cell = [XNBillInfoTableViewCell tabelViewCellWithTableView:tableView ForIndexPath:indexPath];
        cell.itemModel = self.billItemArray[indexPath.row];
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableTypeTableView) {
        NSLog(@"%ld",indexPath.row);
        self.tableTypeTableView.userInteractionEnabled = NO;
        self.tableListTableView.userInteractionEnabled = YES;
        XNBillCountModel *countModel = self.billTypeArray[indexPath.row];
        self.selectBillCode = countModel.billCode;
        self.billItemArray = countModel.itemList;
        [self.contentScrollView setContentOffset:CGPointMake(208, 0) animated:YES];
        self.leftBtn.hidden = NO;
        if (self.billItemArray.count > 0) {
            self.noDataView.hidden = YES;
        }else{
            self.noDataView.hidden = NO;
        }
        [self.tableListTableView reloadData];
    }else{
        XNBillItemModel *itemModel = self.billItemArray[indexPath.row];
        XNBaseBillInfoViewController *vc = [XNUtil getControllerWithOrderId:itemModel.orderId];
        vc.title = itemModel.billName;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}

- (void)selectType:(UIButton *)sender
{
    self.tableTypeTableView.userInteractionEnabled = YES;
    self.tableListTableView.userInteractionEnabled = NO;
    [self.contentScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    self.leftBtn.hidden = YES;
}

- (void)loginOut:(UIButton *)sender
{
    [SVProgressHUD show];
    XNLoginOutRequest *request = [[XNLoginOutRequest alloc] init];
    [request startWithCompletionBlockWithSuccess:^(__kindof XNLoginOutRequest * _Nonnull request) {
        [SVProgressHUD dismiss];
        if (request.success) {
            [self.timer invalidate];
            self.timer = nil;
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"account"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loginToken"];
            [UIApplication sharedApplication].keyWindow.rootViewController = [[XNLoginViewController alloc] init];
        }
    } failure:^(__kindof XNLoginOutRequest * _Nonnull request) {
        [SVProgressHUD dismiss];
        [XNHud showToastWithText:@"请求失败"];
    }];
    

}

- (void)dealloc
{
    NSLog(@"dealloc");
}


@end
